package com.boil.qz.safekavass.web.controller;

import com.boil.pojo.Page2Vo;
import com.boil.pojo.ResultVo;
import com.boil.qz.safekavass.pojo.LearnCountVo;
import com.boil.qz.safekavass.pojo.LearnPersonVo;
import com.boil.qz.safekavass.service.QuestionResRecordService;
import com.boil.qz.safekavass.web.request.param.LearnCountByLearnMonthParam;
import com.boil.qz.safekavass.web.request.param.LearnPersonListParam;
import com.boil.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by ChenYong on 2017-05-23.
 * <br>
 * 学习统计 Controller。
 */
@Controller
@RequestMapping("/web/learn")
public class LearnCountController extends OkBaseController {
    /**
     * 注入题库学习记录 Service
     */
    @Autowired
    private QuestionResRecordService questionResRecordService;

    /**
     * 学习统计页面。
     *
     * @param model 模型
     * @return 学习统计页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "/count.html")
    public String countHtml(Model model) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();
        // 年份集合
        List<Integer> years = new ArrayList<Integer>();
        // 开始年份
        int startYear = Constants.VALUE_2017;
        // 结束年份
        int endYear = Integer.valueOf(DateUtils.getCurrDateStr(Constants.DATE_FORMAT_4));
        // 当前月份
        int currMonth = Integer.valueOf(DateUtils.getCurrDateStr(Constants.DATE_FORMAT_5));

        if (startYear == endYear) {
            years.add(startYear);
        } else {
            for (int i = startYear; i <= endYear; i++) {
                years.add(i);
            }
        }

        resultVo.getResults().add(years);
        resultVo.getResults().add(currMonth);

        model.addAttribute(Constants.RESULT_VO, resultVo);

        return "/learn/count";
    }

    /**
     * 学习统计 Json。
     *
     * @param model 模型
     * @param param 根据学习月份统计学习次数的请求参数
     * @return 学习统计 Json
     */
    @RequestMapping(method = RequestMethod.GET, value = "/count.json")
    @ResponseBody
    public ResultVo countJson(Model model, @Valid LearnCountByLearnMonthParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();
        // 学习月份
        Date learMonth = DateUtils.parse(param.getLearnMonth(), Constants.DATE_FORMAT_6);
        // 根据学习月份查询学习统计
        List<LearnCountVo> learnCountVos = questionResRecordService.findLearnCountByLearnTime(DateUtils.getFirstDayStrInMonth(learMonth, Constants.DATE_FORMAT_1), DateUtils.getLastDayStrInMonth(learMonth, Constants.DATE_FORMAT_1));

        // 日期格式化
        for (LearnCountVo learnCountVo : learnCountVos) {
            learnCountVo.setLearnTime(DateUtils.format(DateUtils.parse(learnCountVo.getLearnTime(), Constants.DATE_FORMAT_1), Constants.DATE_FORMAT_3));
        }

        resultVo.getResults().add(DateUtils.format(learMonth, Constants.DATE_FORMAT_2));
        resultVo.getResults().add(learnCountVos);

        return resultVo;
    }

    /**
     * 获取学习人列表 json。
     *
     * @param model  模型
     * @param param  学习人列表的请求参数
     * @param errors 请求参数的校验结果
     * @return 学习人列表 json
     */
    @RequestMapping(method = RequestMethod.GET, value = "/person/list.json")
    @ResponseBody
    public ResultVo personListJson(Model model, @Valid LearnPersonListParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();

        // 判断请求参数是否校验通过
        Utils.hasRequestParamError(resultVo, errors);

        // 查询分页实例
        Page2<LearnPersonVo> page2 = Utils.getPage2(request, param, Constants.ID_STR);

        page2.getParams().put("learnPerson", param.getLearnPerson());

        // 如果为空，则查询当天的学习人
        if (StringUtils.isBlank(param.getLearnDate())) {
            page2.getParams().put("learnDate", DateUtils.getCurrDateStr(Constants.DATE_FORMAT_1));

            // 不为空，则查询指定学习日期的学习人
        } else {
            page2.getParams().put("learnDate", param.getLearnDate());
        }

        // 分页查询
        questionResRecordService.findLearnPersonByLearnTimeInPage(page2);

        // 获取 DataTables 专用分页映射实例
        Page2Vo<LearnPersonVo> page2Vo = Utils.getPage2Vo(param, page2);

        resultVo.setResult(page2Vo);

        return resultVo;
    }
}